home *** CD-ROM | disk | FTP | other *** search
- Path: erinews.ericsson.se!usenet
- From: Otmar Conradus <ETM.ETMCOOT@MEMO.ERICSSON.SE>
- Newsgroups: comp.lang.c++
- Subject: Re: A simple "find the bug" (please! I need help :)
- Date: Thu, 22 Feb 1996 10:48:55 -0800
- Organization: Ericsson
- Message-ID: <312CBA97.32BF@MEMO.ERICSSON.SE>
- References: <4gdr6n$6p0@guava.epix.net>
- NNTP-Posting-Host: etmpc905.etm.ericsson.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Jon wrote:
- >
- > I wrote this just now and it's telling me that when i try to cout the
- > result at the bottom, it is an undefined value even though I did return
- > the result var... See if you can help.
- >
- > #include <iostream.h>
- >
- > float num1;
- > float num2;
- >
- > getnums () {
- >
- > float result = num1 +num2;
- >
- > cout << "What number?\n";
- > cin >> num1;
- > cout << "And?\n";
- > cin >> num2;
- >
- > return result;
- > }
- >
- > main (){
- >
- > getnums ();
- >
- > cout << result;
- > }
- >
- > ^^^^^^ right there is the result i want printed but it does not
- > recognize it even though I did try to return the value in the function
- > getnums... am I missing something?
- >
- > Jon
-
-
- 1. You are using 2 global variables num1 and num2 but you didn't declare
- a variable result.
-
- 2. If you want your function getnums to return a value you must declare it
- as:
- float getnums()
- and in your main procedure call it as:
- result = getnums();
-
- 3. So your whole program can look like this:
- #include <iostream.h>
-
-
- float getnums () {
- float num1; // abstain from using global variables if you
- float num2; // don't need them globally
- float result; // result is only known in this procedure
-
- cout << "What number?\n"; // read num1 and num2
- cin >> num1;
- cout << "And?\n";
- cin >> num2;
-
- result = num1 + num2; // calculate result
-
- return result;
- }
-
- main (){
- float result; // result in only known in main
- result = getnums();
- cout << result;
- }
-
-
- Otmar Conradus
- i24697@stuhi.ptf.hwb.nl
-